package com.xiangge.qicaixianhua;

import java.util.Scanner;

/**
 * @author xiyu
 * @version 1.0
 * @date 2025/3/18 16:34
 * @description 鲜花管理类
 */
public class FlowerMgr {

    public static void findAll(Flower[] flowers){
        System.out.println("编号\t\t鲜花名称\t\t\t\t销售数量\t价格\t\t\t销售日期\t\t\t销售员\t\t备注");
        for (Flower flower : flowers) {
            if (flower==null){
                continue;
            }
            flower.getInfo();
        }
    }
    public static void revise(Flower[] flowers){
        System.out.print("请输入要修改的订单编号：");
        Scanner input = new Scanner(System.in);
        String id = input.next();
        boolean flag = true;
        for (Flower flower : flowers) {
            if (flower.getId()==null){
                continue;
            }
            if (flower.getId().equals(id)){
                flag = false;
                System.out.println("您要修改的订单信息如下");
                System.out.println("编号\t\t鲜花名称\t\t\t\t销售数量\t价格\t\t\t销售日期\t\t\t销售员\t\t备注");
                flower.getInfo();
                Flower f1 = new Flower();
                System.out.println("请输入修改后的鲜花名称:");
                f1.setName(input.next());
                System.out.println("请输入修改后的销售数量:");
                f1.setShuliang(input.nextInt());
                System.out.println("请输入修改后的鲜花价格:");
                f1.setPrice(input.nextDouble());
                System.out.println("请输入修改后的销售日期: (yyyy-MM-dd)");
                f1.setDate(input.next());
                System.out.println("请输入修改后的销售员:");
                f1.setXiaoshou(input.next());
                System.out.println("请输入修改后的备注:");
                f1.setBeizhu(input.next());
                System.out.println("您确定要修改吗?(y/n)");
                String next = input.next();
                if (next.equals("y")){
                    flower.setName(f1.getName());
                    flower.setShuliang(f1.getShuliang());
                    flower.setPrice(f1.getPrice());
                    flower.setDate(f1.getDate());
                    flower.setXiaoshou(f1.getXiaoshou());
                    flower.setBeizhu(f1.getBeizhu());
                    System.out.println("修改成功");
                    break;
                }else {
                    System.out.println("取消修改");
                    break;
                }
            }
        }
        if (flag){
            System.out.println("对不起，您要修改的订单号不存在！");
        }
    }

    public static void del(Flower[] flowers){
        System.out.print("请输入要删除的订单编号：");
        Scanner input = new Scanner(System.in);
        String id = input.next();
        boolean flag = true;
        for (int i = 0; i < flowers.length; i++) {
            if (flowers[i]==null){
                continue;
            }
            if (flowers[i].getId().equals(id)){
                flag = false;
                System.out.println("您要删除的订单信息如下");
                System.out.println("编号\t\t鲜花名称\t\t\t\t销售数量\t价格\t\t\t销售日期\t\t\t销售员\t\t备注");
                flowers[i].getInfo();
                System.out.println("您确定要删除吗?(y/n)");
                String next = input.next();
                if (next.equals("y")){
                    flowers[i]=null;
                    System.out.println("删除成功");
                    break;
                }else if (next.equals("n")){
                    System.out.println("取消删除");
                    break;
                }
            }
        }
        if (flag){
            System.out.println("对不起，您要删除的订单号不存在！");
        }
    }

    public static void add(Flower[] flowers) {
        System.out.println("请输入需要添加的订单信息：");
        Scanner input = new Scanner(System.in);
        boolean errflag = false;
        for (int i = 0; i < flowers.length; i++) {
            if (flowers[i]==null){
                Flower f1 = new Flower();
                System.out.println("请输入id:");
                f1.setId(input.next());
                System.out.println("请输入鲜花名称:");
                f1.setName(input.next());
                System.out.println("请输入销售数量:");
                f1.setShuliang(input.nextInt());
                errflag = err(f1);
                if (errflag){
                    break;
                }

                System.out.println("请输入鲜花价格:");
                f1.setPrice(input.nextDouble());
                errflag = err(f1);
                if (errflag){
                    break;
                }
                System.out.println("请输入销售日期: (yyyy-MM-dd)");
                f1.setDate(input.next());
                System.out.println("请输入销售员:");
                f1.setXiaoshou(input.next());
                System.out.println("请输入备注:");
                f1.setBeizhu(input.next());

                flowers[i]=f1;
                System.out.println("添加完成");
                break;
            }
        }
    }
    public static boolean err(Flower flower){
        if (flower.getShuliang()<0||flower.getPrice()<0){
            System.err.println("不能为负数");
            return true;
        }
        return false;
    }
}
